﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataEntity;
using Model;

namespace SWA_View
{
    public partial class ProjectInfo : Form
    {
        private int _id = 0;
        public ProjectInfo(int ProjectID, DictionaryEnum ActionType)
        {
            InitializeComponent();
            _id = ProjectID;
            if (_id > 0)
            {
                //ProjectModel pm = new ProjectModel();
                //pe = pm.GetProjectEntity("id =" + _id).FirstOrDefault();
                CommonModel model = new CommonModel();
                pe = model.Query<ProjectEntity>("id =" + _id).FirstOrDefault();
            }
            switch (ActionType)
            {
                case DictionaryEnum.New://新建
                    break;
                case DictionaryEnum.View://查看
                    btnOK.Enabled = false;
                    SetData(pe, true);
                    break;
                case DictionaryEnum.Edit://编辑
                    btnOK.Text = "修改";
                    SetData(pe, false);
                    break;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.GetProjectEntity();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        public void SetData(ProjectEntity pe, bool IsEnabled)
        {
            if (pe == null)
                pe = new ProjectEntity() { CreateDate = DateTime.Now, ID = _id };
            txtPrjNo.Text = pe.ProjectNo;
            txtPrjNo.ReadOnly = IsEnabled;

            txtPrjLeader.Text = pe.ProjectHost;
            txtPrjLeader.ReadOnly = IsEnabled;

            txtModelNo.Text = pe.ModelNo;
            txtModelNo.ReadOnly = IsEnabled;

            txtClient.Text = pe.Client;
            txtClient.ReadOnly = IsEnabled;

            txtScale.Text = pe.Rate;
            txtScale.ReadOnly = IsEnabled;

            comboShipType.SelectedItem = pe.ShipType;
            comboShipType.Enabled = !IsEnabled;

            txtRealLength.Text = pe.TotalLengthShip+"";
            txtRealLength.ReadOnly = IsEnabled;
            txtModelLength.Text = pe.TotalLengthModel + "";
            txtModelLength.ReadOnly = IsEnabled;

            txtRealLiangzhuJianchang.Text = pe.DistenthShip+"";
            txtRealLiangzhuJianchang.ReadOnly = IsEnabled;
            txtModelLiangzhuJianchang.Text = pe.DistenthModel + "";
            txtModelLiangzhuJianchang.ReadOnly = IsEnabled;

            txtRealXingkuan.Text = pe.WidthShip + "";
            txtRealXingkuan.ReadOnly = IsEnabled;
            txtModelXingkuan.Text = pe.WidthModel + "";
            txtModelXingkuan.ReadOnly = IsEnabled;

            txtRealHeadDraft.Text = pe.ShouChishuiShip + "";
            txtRealHeadDraft.ReadOnly = IsEnabled;
            txtModelHeadDraft.Text = pe.ShouChishuiModel + "";
            txtModelHeadDraft.ReadOnly = IsEnabled;

            txtRealTailDraft.Text = pe.WeiChishuiShip + "";
            txtRealTailDraft.ReadOnly = IsEnabled;
            txtModelTailDraft.Text = pe.WeiChishuiModel + "";
            txtModelTailDraft.ReadOnly = IsEnabled;

            txtRealXingshen.Text = pe.XingShengShip + "";
            txtModelXingshen.Text = pe.XingShengModel + "";
            txtModelXingshen.ReadOnly = IsEnabled;
            txtRealXingshen.ReadOnly = IsEnabled;

            txtRealZhougao.Text = pe.ZhouGaoShip + "";
            txtModelZhougao.Text = pe.ZhouGaoModel + "";
            txtModelZhougao.ReadOnly = IsEnabled;
            txtRealZhougao.ReadOnly = IsEnabled;

            txtRealZhouJianju.Text = pe.ZhouJianJuShip + "";
            txtModelZhouJianju.Text = pe.ZhouJianJUModel + "";
            txtModelZhouJianju.ReadOnly = IsEnabled;
            txtRealZhouJianju.ReadOnly = IsEnabled;

            txtFangxingXishu.Text = pe.FangXingIndex + "";
            txtFangxingXishu.ReadOnly = IsEnabled;
        }

        ProjectEntity pe = null;
        public ProjectEntity GetProjectEntity()
        {
            if(pe == null)
                pe = new ProjectEntity();
            pe.ProjectNo = txtPrjNo.Text;
            pe.ProjectHost = txtPrjLeader.Text;
            pe.ModelNo = txtModelNo.Text;
            pe.Rate = txtScale.Text;
            if (comboShipType.SelectedItem != null)
                pe.ShipType = comboShipType.SelectedItem.ToString();
            else
            {
                MessageBox.Show("必须选择船舶类型", "", MessageBoxButtons.OK);
                return null;
            }

            pe.Client = txtClient.Text;
            try
            {
                pe.TotalLengthShip = float.Parse(txtRealLength.Text);
                pe.TotalLengthModel = float.Parse(txtModelLength.Text);

                pe.DistenthShip = float.Parse(txtRealLiangzhuJianchang.Text);
                pe.DistenthModel = float.Parse(txtModelLiangzhuJianchang.Text);

                pe.WidthShip = float.Parse(txtRealXingkuan.Text);
                pe.WidthModel = float.Parse(txtModelXingkuan.Text);

                pe.ShouChishuiShip = float.Parse(txtRealHeadDraft.Text);
                pe.ShouChishuiModel = float.Parse(txtRealHeadDraft.Text);

                pe.WeiChishuiShip = float.Parse(txtRealTailDraft.Text);
                pe.WeiChishuiModel = float.Parse(txtModelTailDraft.Text);

                pe.XingShengShip = float.Parse(txtRealXingshen.Text);
                pe.XingShengModel = float.Parse(txtModelXingshen.Text);

                pe.ZhouGaoShip = float.Parse(txtRealZhougao.Text);
                pe.ZhouGaoModel = float.Parse(txtModelZhougao.Text);

                pe.ZhouJianJuShip = float.Parse(txtRealZhouJianju.Text);
                pe.ZhouJianJUModel = float.Parse(txtModelZhouJianju.Text);

                pe.FangXingIndex = float.Parse(txtFangxingXishu.Text);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

            ProjectModel pm = new ProjectModel();
            CommonModel model = new CommonModel();
            if (_id <= 0)
            {
                //int id = 0;//pm.Save(pe);
                //ChiShuiModel model = new ChiShuiModel();
                int id = model.Save<ProjectEntity>(pe);
                if (id > 0)
                {
                    pe.ID = id;
                    MessageBox.Show("保存成功!", "", MessageBoxButtons.OKCancel);
                }
                else
                {
                    MessageBox.Show("保存失败!", "", MessageBoxButtons.OKCancel);
                    return null;
                }
            }
            else
            {
                //pm.Update(pe);
                model.Update<ProjectEntity>(pe);
                MessageBox.Show("更新成功!", "", MessageBoxButtons.OKCancel);                
            }
            
               
            return pe;
        }

        private void txtRealLength_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                try
                {
                    double.Parse(tb.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("只能输入数字");
                    tb.Focus();
                    tb.Text = "0";
                }
            }
        }
    }
}
